package com.atguigu.controller;

import com.atguigu.common.utils.R;
import com.atguigu.entity.Course;
import com.atguigu.entity.vo.CourseQuery;
import com.atguigu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "课程表")
@ApiSort(3)
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    // 1、查询所有数据
    @ApiOperation("查询所有数据")
    @ApiOperationSupport(order = 1)
    @GetMapping("list")
    public R findAll() {
        List<Course> list = courseService.list(null);
        return R.ok().data("items", list);
    }

    // 2、分页查询
    @ApiOperation("分页查询")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true, defaultValue = "1", paramType = "path", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页记录数", required = true, defaultValue = "10", paramType = "path", dataType = "Long")
    })
    @GetMapping("page/{current}/{limit}")
    public R findPage(
            @PathVariable long current,
            @PathVariable long limit) {
        Page<Course> page = new Page<>(current, limit);
        courseService.page(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("rows", page.getRecords());
        return R.ok().data(map);
    }

    // 3、条件分页查询
    @ApiOperation("条件分页查询")
    @ApiOperationSupport(order = 3)
    @PostMapping("page2/{current}/{limit}")
    public R findPage2(
            @ApiParam(name = "current", value = "当前页", required = true, defaultValue = "1")
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true, defaultValue = "10")
            @PathVariable long limit,
            @RequestBody(required = false) CourseQuery courseQuery) {
        Page<Course> page = new Page<>(current, limit);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getId())) {
            wrapper.eq("id", courseQuery.getId());
        }
        if (!StringUtils.isEmpty(courseQuery.getTitle())) {
            wrapper.like("title", courseQuery.getTitle());
        }
        if (!StringUtils.isEmpty(courseQuery.getTeacherId())) {
            wrapper.eq("teacher_id", courseQuery.getTeacherId());
        }
        if (!StringUtils.isEmpty(courseQuery.getOrderBy())) {
            if ("desc".equalsIgnoreCase(courseQuery.getOrder())) {
                wrapper.orderByDesc(courseQuery.getOrderBy());
            } else {
                wrapper.orderByAsc(courseQuery.getOrderBy());
            }
        }
        courseService.page(page, wrapper);
        return R.ok().data("total", page.getTotal()).data("rows", page.getRecords());
    }

    // 4、添加操作
    @ApiOperation("添加操作")
    @ApiOperationSupport(order = 4)
    @PostMapping("add")
    public R addCourse(@RequestBody Course course) {
        boolean flag = courseService.save(course);
        return flag ? R.ok() : R.error();
    }

    // 5、根据ID查询
    @ApiOperation("根据ID查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "主键id", required = true)
            @PathVariable Integer id) {
        Course course = courseService.getById(id);
        return R.ok().data("course", course);
    }

    // 6、修改操作
    @ApiOperation("修改操作")
    @ApiOperationSupport(order = 6)
    @PostMapping("edit")
    public R editCourse(@RequestBody Course course) {
        boolean flag = courseService.updateById(course);
        return flag ? R.ok() : R.error();
    }

    // 7、删除操作
    @ApiOperation("删除操作")
    @ApiOperationSupport(order = 7)
    @DeleteMapping("{id}")
    public R removeCourse(
            @ApiParam(name = "id", value = "主键id", required = true)
            @PathVariable Integer id) {
        boolean flag = courseService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    // 8、查询分页数据(旧)
    @ApiIgnore
    @ApiOperation("查询分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true, defaultValue = "1", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页记录数", required = true, defaultValue = "10", paramType = "query", dataType = "Long")
    })
    @PostMapping("page3")
    public R findPageList(
            long current,
            long limit,
            @RequestBody(required = false) CourseQuery courseQuery
    ) {
        /*List<Map<String, Object>> list = courseService.getPageList(current, limit, courseQuery);
        if (list == null) {
            return R.error();
        } else {
            return R.ok().data("items", list);
        }*/
        Page<Course> page = new Page<>(current, limit);
        List<Course> list = courseService.getPageList(page, courseQuery);
        return R.ok().data("data", page);
    }

    // 9、不分页显示
    @PostMapping("list3")
    public R findNoPageList(@RequestBody(required = false) CourseQuery courseQuery) {
        List<Course> list = courseService.getNoPageList(courseQuery);
        if (list == null) {
            return R.error();
        } else {
            return R.ok().data("items", list);
        }
    }
}

